package com.itboye.textillexepres.db;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.itboye.textileexpes.beans.CityBeans;
import com.itboye.textileexpes.beans.CityModel;
import com.itboye.textileexpres.R;

/**
 * 保存草稿
 * 
 * @author admin
 * 
 */
public class DBManager {

	DBHelper mDbHelper;
	SQLiteDatabase db;
	Context mContext;
	public static final String DB_NAME = "itboyetext.db";
	public static final String DB_PATH = "/data"
			+ Environment.getDataDirectory().getAbsolutePath() + "/"
			+ "com.itboye.textileexpres"; // 存放路径
	private final int BUFFER_SIZE = 400000;

	public DBManager(Context mContext) {
		this.mContext = mContext;
		mDbHelper = new DBHelper(mContext);
	}

	/**
	 * 被调用方法
	 */
	public void openDateBase() {
		this.db = this.openDateBase(DB_PATH + "/" + DB_NAME);

	}

	/**
	 * 被调用方法
	 */
	public void closeDateBase() {
		if (this.db != null)
			this.db.close();

	}

	/**
	 * 打开数据库
	 * 
	 * @param dbFile
	 * @return SQLiteDatabase
	 * @author sy
	 */
	private SQLiteDatabase openDateBase(String dbFile) {
		File file = new File(dbFile);
		if (!file.exists()) {
			// // 打开raw中得数据库文件，获得stream流
			InputStream stream = mContext.getResources().openRawResource(
					R.raw.itboyetext);
			try {

				// 将获取到的stream 流写入道data中
				FileOutputStream outputStream = new FileOutputStream(dbFile);
				byte[] buffer = new byte[BUFFER_SIZE];
				int count = 0;
				while ((count = stream.read(buffer)) > 0) {
					outputStream.write(buffer, 0, count);
				}
				outputStream.close();
				stream.close();
				SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile,
						null);
				return db;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return db;
	}

	public String queryCityNo(String cityName) {
		// db = mDbHelper.getReadableDatabase();
		Cursor cursor = null;
		if (this.db != null)
			cursor = db.query("hat_city", new String[] { "cityID" }, "city=?",
					new String[] { cityName }, null, null, null);
		String cityCode = null;
		if (cursor != null)
			while (cursor.moveToNext()) {
				cityCode = cursor.getString(0);
			}
		cursor.close();
		db.close();
		return cityCode;
	}

	/***
	 * 查询区域列表
	 * 
	 * @param cityName
	 * @return
	 */
	public ArrayList<CityBeans> queryArea(String cityNo) {

		Cursor cursor = null;
		ArrayList<CityBeans> arrayArea = new ArrayList<CityBeans>();
		if (this.db != null)
			// cursor=db.rawQuery(", selectionArgs)
			cursor = db.query("hat_area", null, "father like ?",
					new String[] { cityNo + "%" }, null, null, null);
		if (cursor != null)
			while (cursor.moveToNext()) {
				CityBeans cityModel = new CityBeans();
				cityModel.setAreaCode(cursor.getString(cursor
						.getColumnIndexOrThrow("areaID")));
				cityModel.setCityCode(cursor.getString(cursor
						.getColumnIndexOrThrow("father")));
				cityModel.setCountry_name(cursor.getString(cursor
						.getColumnIndexOrThrow("area")));
				arrayArea.add(cityModel);
			}
		cursor.close();
		db.close();
		return arrayArea;
	}

	public String queryPriovinceNo(String city) {
		// TODO Auto-generated method stub

		db = mDbHelper.getReadableDatabase();
		Cursor cursor = null;
		if (this.db != null)
			cursor = db.query("hat_province", new String[] { "provinceID" },
					"province=?", new String[] { city }, null, null, null);
		String cityCode = null;
		if (cursor != null)
			while (cursor.moveToNext()) {
				cityCode = cursor.getString(0);
			}
		cursor.close();
		db.close();
		return cityCode;
	}

	
}
